<template>
  <Tree
    :data="resourceTreeData"
    ref="tree"
    @on-check-change="checkChanged"
    show-checkbox
    style="overflow-y:auto"
  ></Tree>
</template>

<script>
import axios from '@/libs/api.request'
export default {
  props: ['row', 'treeData'],
  data () {
    return {
      resourceTreeData: [],
      roleData: {},
      resourceIds: []
    }
  },
  methods: {
    handleOk () {
      if (this.resourceIds.length === 0) {
        this.$emit('on-success-resourceAuth')
      } else {
        axios
          .request({
            method: 'post',
            url: 'admin/rs/role/resourceAuth',
            data: {
              resourceIds: this.resourceIds,
              roleId: this.roleData.tid
            }
          })
          .then(response => {
            if (response.data.code === 200) {
              this.$Message.success('角色分配成功')
              this.$emit('on-success-resourceAuth')
            } else {
              this.$Message.error(response.data.msg)
            }
          })
          .catch(response => {
            this.$Message.error('程序错误')
          })
      }
    },
    checkChanged (nodes) {
      var temp = []
      for (var i = 0; i < nodes.length; i++) {
        temp.push(nodes[i].tid)
      }
      this.resourceIds = temp
    }
  },
  watch: {
    row (v) {
      this.roleData = JSON.parse(JSON.stringify(v))
    },
    treeData (v) {
      this.resourceTreeData = JSON.parse(JSON.stringify(v))
    }
  }
}
</script>
